package net.oschina.ivansong.netty.learn.test;

import org.junit.Test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.charset.StandardCharsets;

public class NIOFileExam {
    @Test
    public void testBuffer() {
        String msg = "Hello World 你好世界";
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
        byte[] msgBytes = msg.getBytes(StandardCharsets.UTF_8);
        byteBuffer.put(msgBytes);
        byteBuffer.flip();

        byte[] newBytes = new byte[msgBytes.length];
        int i = 0;
        while (byteBuffer.hasRemaining()) {
            newBytes[i++] = byteBuffer.get();
        }

        System.out.println(new String(newBytes, StandardCharsets.UTF_8));
    }

    //@Test
    public void testFileChannel() {
        File file = new File("1.txt");
        try (FileInputStream inputStream = new FileInputStream(file); FileChannel fileInChannel = inputStream.getChannel();
             FileOutputStream outputStream = new FileOutputStream("2.txt"); FileChannel fileOutChannel = outputStream.getChannel()) {
            ByteBuffer byteBuffer = ByteBuffer.allocate((int) file.length());
            fileInChannel.read(byteBuffer);
            byteBuffer.flip();
            fileOutChannel.write(byteBuffer);
        } catch (Exception ignored) {

        }
    }

    @Test
    public void testFileCp1() {
        File file = new File("E:\\GoogleDownloads\\ideaIC-2019.2.4.win.zip");
        long starTime = System.currentTimeMillis();
        try (FileInputStream inputStream = new FileInputStream(file); FileChannel fileInChannel = inputStream.getChannel();
             FileOutputStream outputStream = new FileOutputStream("E:\\GoogleDownloads\\1.zip"); FileChannel fileOutChannel = outputStream.getChannel()) {
            //创建一个直接缓冲区
            ByteBuffer byteBuffer = ByteBuffer.allocateDirect(5 * 1024 * 1024);
            //创建一个非直接缓冲区
            //ByteBuffer byteBuffer = ByteBuffer.allocate(5 * 1024 * 1024);
            //写入到缓冲区
            while (fileInChannel.read(byteBuffer) != -1) {
                //切换读模式
                byteBuffer.flip();
                fileOutChannel.write(byteBuffer);
                byteBuffer.clear();
            }

            long endTime = System.currentTimeMillis();
            System.out.println("消耗时间：" + (endTime - starTime) + "毫秒");
        } catch (Exception ignored) {

        }
    }


    @Test
    public void testFileCp2() {
        File file = new File("E:\\GoogleDownloads\\ideaIC-2019.2.4.win.zip");
        long starTime = System.currentTimeMillis();
        try (FileInputStream inputStream = new FileInputStream(file); FileChannel fileInChannel = inputStream.getChannel();
             FileOutputStream outputStream = new FileOutputStream("E:\\GoogleDownloads\\2.zip"); FileChannel fileOutChannel = outputStream.getChannel()) {
            //创建一个直接缓冲区
            //ByteBuffer byteBuffer = ByteBuffer.allocateDirect(5 * 1024 * 1024);
            //创建一个非直接缓冲区
            ByteBuffer byteBuffer = ByteBuffer.allocate(5 * 1024 * 1024);
            //写入到缓冲区
            while (fileInChannel.read(byteBuffer) != -1) {
                //切换读模式
                byteBuffer.flip();
                fileOutChannel.write(byteBuffer);
                byteBuffer.clear();
            }

            long endTime = System.currentTimeMillis();
            System.out.println("消耗时间：" + (endTime - starTime) + "毫秒");
        } catch (Exception ignored) {

        }
    }


    @Test
    public void testFileCp3() {
        File file = new File("E:\\GoogleDownloads\\ideaIC-2019.2.4.win.zip");
        long starTime = System.currentTimeMillis();
        try (FileInputStream inputStream = new FileInputStream(file);
             FileOutputStream outputStream = new FileOutputStream("E:\\GoogleDownloads\\3.zip")) {

            byte[] bytes = new byte[5 * 1024 * 1024];
            while (inputStream.read(bytes) != -1) {
                outputStream.write(bytes);
                bytes = new byte[5 * 1024 * 1024];
            }
            outputStream.flush();
            long endTime = System.currentTimeMillis();
            System.out.println("消耗时间：" + (endTime - starTime) + "毫秒");
        } catch (Exception ignored) {

        }
    }
}
